package com.timi_licai.timi_licai_account.utils;

import android.database.Cursor;

import com.timi_licai.timi_licai_account.MyConstants;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.model.ConsumeDetails;

import java.util.LinkedList;
import java.util.List;

/**
 * 数据库常用的查找方法
 */
public class DatabaseQueryUtils {


    /**
     * 得到指定账本的总的收入
     *
     * @param bookid
     * @return
     */
    public static float getInputTotal(int bookid) {
        float ret = -1;
        float sum = 0.0f;
        if (bookid != 0) {
            Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK_DETAILS, null,
                    "bookid=? and io_choose=?", new String[]{bookid + "", "1"}, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    float fee = cursor.getFloat(cursor.getColumnIndex("fee"));
                    sum += fee;
                }
            }
            cursor.close();
        }
        ret = sum;
        return ret;
    }

    /**
     * 得到选中账本的总的支出
     *
     * @param bookid
     * @return
     */
    public static float getOutputTotal(int bookid) {
        float ret = -0.1f;
        float sum = 0.0f;
        if (bookid != 0) {
            Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK_DETAILS, null,
                    "bookid=? and io_choose=?", new String[]{bookid + "", "0"}, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    float fee = cursor.getFloat(cursor.getColumnIndex("fee"));
                    sum += fee;
                }
            }
            cursor.close();
        }
        ret = sum;
        return ret;
    }


    /**
     * 得到选中的账本的id
     *
     * @return
     */
    public static int getSelectedAccountBook() {
        int ret = 0;
        Cursor account_id = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK,
                null,
                "ismark=?", new String[]{1 + ""}, null);

        if (account_id != null) {
            while (account_id.moveToNext()) {
                ret = account_id.getInt(account_id.getColumnIndex("_id"));
            }
            account_id.close();
        }
        return ret;
    }

    /**
     * 根据bookid  得到选中账本的名字
     *
     * @param bookid
     * @return
     */
    public static String getSelectedAccountBookName(int bookid) {
        String ret = null;
        if (bookid != 0) {
            Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK, null, "_id=?", new String[]{bookid + ""}, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    ret = cursor.getString(cursor.getColumnIndex("bookname"));
                }
            }
            cursor.close();
        }
        return ret;
    }

    public static List<ConsumeDetails> getConsumeDetails(int bookid) {
        List<ConsumeDetails> detailsList = new LinkedList<>();
        if (bookid != 0) {
            Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK_DETAILS,
                    null, "bookid = ?", new String[]{bookid + ""}, "createtime asc");
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    ConsumeDetails details = new ConsumeDetails();
                    String createtime = cursor.getString(cursor.getColumnIndex("createtime"));
                    String description = cursor.getString(cursor.getColumnIndex("description"));
                    int io_choose = cursor.getInt(cursor.getColumnIndex("io_choose"));
                    float fee = cursor.getFloat(cursor.getColumnIndex("fee"));
                    int typeid = cursor.getInt(cursor.getColumnIndex("type_id"));
                    details.setCreate_time(createtime);
                    details.setIoChoose(io_choose);
                    details.setDescription(description);
                    details.setFee(fee);
                    details.setType_id(typeid);
                    detailsList.add(details);
                }
                cursor.close();
            }
        }
        return detailsList;
    }

    /**
     * 根据Typeid  来查找typeName
     *
     * @param typeId
     * @return
     */
    public static String getNameByTypeId(int typeId) {
        String ret = null;
        if (typeId != 0) {
            Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_CONSUME_TYPE, null,
                    "_id=?", new String[]{typeId + ""}, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    ret = cursor.getString(cursor.getColumnIndex("typename"));
                }
            }
            cursor.close();
        }
        return ret;
    }
}
